﻿Imports System.Data.OleDb

Public Class Detail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If IsPostBack = False Then
            Dim oledbConn As New OleDbConnection(ConfigurationManager.ConnectionStrings("lensPicConnectionString").ConnectionString)
            oledbConn.Open()
            Dim doesLikeOrNotSql As String = "SELECT * FROM Likes WHERE PictureId=@f1 AND Reader=@f2"
            Dim doesLikeOrNotCmd As OleDbCommand = New OleDbCommand(doesLikeOrNotSql, oledbConn)
            doesLikeOrNotCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            doesLikeOrNotCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            Dim dr As OleDbDataReader = doesLikeOrNotCmd.ExecuteReader()
            If (dr.HasRows()) Then
                btn_like.Visible = False
                lbl_like.Visible = True
            Else
                btn_like.Visible = True
                lbl_like.Visible = False
            End If
        End If

        If IsPostBack Then Return
        'meaning if it is a post back, then we jump out of function and do not run the rest below
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("lensPicConnectionString").ConnectionString)
        Dim sql = "INSERT INTO PicturesViewedByUsers (Picturefkid, MemberName) VALUES (@f1,@f2)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.ExecuteNonQuery()
        conn.Close()

    End Sub



    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("lensPicConnectionString").ConnectionString)
        Dim sql = "INSERT INTO Comments (Author,Title,Comment,PictureID) VALUES (@f1,@f2,@f3,@f4)"
        Dim cmd = New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("ID"))
        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("Detail.aspx?ID=" & Request.Params("ID"))
    End Sub

    
    Protected Sub GridView2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView2.SelectedIndexChanged

    End Sub

    Protected Sub btn_SaveRating_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_SaveRating.Click
        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("lensPicConnectionString").ConnectionString)
        conn.Open()
        Dim rmSql = "DELETE FROM PictureRatings WHERE Rater = @f1 AND pictureFkid=@f2"
        Dim rmCmd As New OleDbCommand(rmSql, conn)
        rmCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        rmCmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        rmCmd.ExecuteNonQuery()
        conn.Close()


        Dim sql = "INSERT INTO PictureRatings(Rating,pictureFkid,Rater) VALUES (@f1,@f2,@f3)"
        Dim addCmd As New OleDbCommand(sql, conn)
        conn.Open()
        addCmd.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        addCmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        addCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        addCmd.ExecuteNonQuery()
        conn.Close()

        lbl_ratingFeedBack.Text = "Thank You for Adding your Rating"
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_like.Click
        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("lensPicConnectionString").ConnectionString)
        conn.Open()
        Dim addLikeSql As String = "INSERT INTO Likes(PictureId,Reader) VALUES (@f1,@f2)"
        Dim addLikeCmd As OleDbCommand = New OleDbCommand(addLikeSql, conn)
        addLikeCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        addLikeCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        addLikeCmd.ExecuteNonQuery()
        btn_like.Visible = False
        lbl_like.Visible = True

    End Sub

    Protected Sub btn_setTag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_setTag.Click
        Dim pictureId = Request.Params("ID")
        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("lensPicConnectionString").ConnectionString)
        conn.Open()
        For Each li As ListItem In ddl_pictureTag.Items
            If li.Selected = True Then
                Dim sql = "INSERT INTO PictureTagJunction (tagfkid, picturefkid) VALUES (@f1,@f2)"
                Dim cmd As New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@f1", li.Value)
                cmd.Parameters.AddWithValue("@f2", pictureId)
                cmd.ExecuteNonQuery()
            End If
        Next
        conn.Close()

        lbl_tagLocFeed.Text = "Thank You for tagging Location"
    End Sub

    Protected Sub btn_setTag0_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_setTag0.Click
        Dim pictureIdd = Request.Params("ID")
        Dim rconn As New OleDbConnection(ConfigurationManager.ConnectionStrings("lensPicConnectionString").ConnectionString)
        rconn.Open()
        For Each ri As ListItem In ddl_tagRegion.Items
            If ri.Selected = True Then
                Dim rsql = "INSERT INTO PictureTageRegionJunction (tagRfkid, pictureRfkid) VALUES (@f8,@f9)"
                Dim cmd As New OleDbCommand(rsql, rconn)
                cmd.Parameters.AddWithValue("@f8", ri.Value)
                cmd.Parameters.AddWithValue("@f9", pictureIdd)
                cmd.ExecuteNonQuery()
            End If
        Next
        rconn.Close()

        lbl_regTagFeed.Text = "Thank You for tagging Region"
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        Response.Redirect("Profile.aspx?TravellerName=" & User.Identity.Name)
    End Sub
End Class